package thread;

/**
 *  串行和并发的时间对比
 */
public class ThreadNB {
    private static final long COUNt = 10_0000_0000;

    public static void main(String[] args) throws InterruptedException {
        serial();
        concurrent();
    }

    // 串行进行20亿的累加
    private static void serial() {
        long start = System.nanoTime();
        long a = 0;
        for (long i = 0; i < COUNt; i++) {
            a ++;
        }
        // b的执行需要等待a走完才能进行
        long b = 0;
        for (long i = 0; i < COUNt; i++) {
            b ++;
        }
        long end = System.nanoTime();
        double allTime = (end - start) * 1.0 / 1000 / 1000;
        System.out.println("顺序执行共耗时 : " + allTime + "ms");
    }


    // 并发进行 + 10亿操作
    private static void concurrent() throws InterruptedException {
        long start = System.nanoTime();
        // 子线程进行 + 10亿操作
        Thread thread = new Thread(() -> {
            long a = 0;
            for (long i = 0; i < COUNt; i++) {
                a ++;
            }
        });
        thread.start();
        // 主线程中也执行 + 10亿操作
        long b = 0;
        for (long i = 0; i < COUNt; i++) {
            b ++;
        }
        // 等待子线程执行结束，主线程和子线程的加法操作都完成
        // 等待子线程thread执行结束才能执行下面代码
        thread.join();
        long end = System.nanoTime();
        double allTime = (end - start) * 1.0 / 1000 / 1000;
        System.out.println("并发执行共耗时 : " + allTime + "ms");
    }
}
